package com.yu.work;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.yu.work.domain.Record;
import com.yu.work.service.RecordService;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

/**
 * 修改记录页面
 */
@ContentView(R.layout.activity_edit_record)
public class RecordEditActivity extends AppCompatActivity {

    private static final String EXTRA_RECORD = "com.yu.work.RecordEditActivity.EXTRA_RECORD";
    private Record mRecord;
    private RecordService mRecordService = RecordService.getInstance();

    @ViewInject(R.id.toolbar)
    private Toolbar mToolbar;

    @ViewInject(R.id.tv_record_target_number)
    private TextView tv_targetNumber;

    @ViewInject(R.id.edt_record_remark)
    private EditText edt_remark;

    public static Intent newIntent(Context packageContext, Record record) {
        Intent i = new Intent(packageContext, RecordEditActivity.class);
        i.putExtra(EXTRA_RECORD, record);
        return i;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        mRecord = (Record) getIntent().getSerializableExtra(EXTRA_RECORD);
        tv_targetNumber.setText(String.valueOf(mRecord.getTargetNumber()));
        edt_remark.setText(mRecord.getRemark());

        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    /**
     * 保存按钮点击事件
     * @param v
     */
    @Event(R.id.img_record_save)
    private void drawNumber(View v) {
        String remark = edt_remark.getText().toString();
        mRecord.setRemark(remark);
        mRecordService.update(mRecord);
        Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
        finish();
    }

}
